package com.android.adc.sensors;


public class SensorAnalyzer 
{
	private static int nearProximityValue = 3;
	private static float moveInterval = 0.1f;
	
	private enum ProximityState
	{
		NEAR,
		FAR;
	};
    
    private float[] oldAccelerometrValues;

    private int totalProximityChangesCount = 0;
    private int objectNearCount = 0;
    private int totalAcceleratorChangesCount = 0;
    private int accaleratorChangesCount = 0;
    
    private boolean lastNearState = false;
    private ProximityState lastProximityState = ProximityState.FAR;
    
    private boolean cleared = true;
    
    public SensorAnalyzer()
    {
    	clear();
    }
	
	public void updateProxmity(int proximityValue)
	{
		totalProximityChangesCount++;
		
		if(proximityValue == getNearProxmityValue())
		{
			objectNearCount++;
			lastProximityState = ProximityState.NEAR;
		}
		else
		{
			lastProximityState = ProximityState.FAR;
		}
	}
	
	public void updateAccelerator(float[] values)
	{
		/* Dane zostały zresetowane, nie ma więc jeszcze starych wartości. 
		 * Przepisujemy nowe i kończymy działanie metody
		 */
		if(cleared)
		{
			cleared = false;
			for(int i=0;i<values.length;i++)
				oldAccelerometrValues[i] = values[i];
			
			return;
		}
		
		boolean changed = false;
		
		for(int i=0;i<values.length;i++)
		{
			if(Math.abs(oldAccelerometrValues[i] - values[i]) >= getMoveInterval())
				changed = true;
			
			oldAccelerometrValues[i] = values[i];
		}
		
		totalAcceleratorChangesCount++;
		if(changed)
			accaleratorChangesCount++;
	}

	/**
	 * Metoda zeruje aktualne wyniki analizy i zaczyna od nowa analizować stan komórki
	 */
	public void clear() 
	{
	    totalProximityChangesCount = 0;
	    objectNearCount = 0;
	    totalAcceleratorChangesCount = 0;
	    accaleratorChangesCount = 0;
	    
	    oldAccelerometrValues = new float[] {0,0,0};
	    
	    cleared = true;
	}

	/**
	 * Metoda zwraca analize ruchu komorki wzgledem ostatniego 
	 * zerowania wyników
	 * 
	 * @return
	 */
	public boolean isInMove()
	{
		double acceleratorChangesPercentage = 0;
		double objectNearChangesPercentage = 0;
		
		if(totalAcceleratorChangesCount > 0)
			acceleratorChangesPercentage = (100.0 * accaleratorChangesCount) / totalAcceleratorChangesCount;
		
		if(totalProximityChangesCount > 0)
			objectNearChangesPercentage = (100.0 * objectNearCount) / totalProximityChangesCount;
		
		
		/* Jeżeli komórka jest w ruchu, to na pewno należy podgłosnić dzwonek */
		if(acceleratorChangesPercentage >= 80)
			return true;
		
//		if(totalProximityChangesCount > 0 && totalProximityChangesCount <= 2)
//		{
			if(lastProximityState.equals(ProximityState.FAR))
				lastNearState = false;
			else if(lastProximityState.equals(ProximityState.NEAR))
				lastNearState = true;
//		}
//		else if(totalProximityChangesCount > 2)
//		{
//			if(objectNearChangesPercentage < 25)
//			{
//				lastNearState = false;
//			}
//			else if(objectNearChangesPercentage >= 50)
//			{
//				lastNearState = true;
//			}
//		}
		
		
//		Log.d("Wartosc", "Accelerator: "+acceleratorChangesPercentage);
//		Log.d("Wartosc", "Near: "+objectNearChangesPercentage);
//		Log.d("Wartosc", "LastNear: "+lastNearState+", "+totalProximityChangesCount);
		
		
		
		if(lastNearState)
			return true;
		
		
		return false;
	}

	public boolean hasChanged() 
	{
		return true;
	}
	
	public int getNearProxmityValue()
	{
		return nearProximityValue;
	}
	
	public float getMoveInterval()
	{
		return moveInterval;
	}

}
